﻿using Sustainalytics.Admin.Synchronization.Utility;
using Sustainalytics.Utils;
using System;
using System.Diagnostics.Tracing;
using System.Threading;

namespace Sustainalytics.Portfolios.BL
{
    [EventSource(Name = "Sustainalytics-ClientPlatform-PortfolioManagement")]
    public sealed class PortfolioManagementEventSource : EventSource
    {
        private static Lazy<PortfolioManagementEventSource> _lazyLog = new Lazy<PortfolioManagementEventSource>(InitLog, LazyThreadSafetyMode.ExecutionAndPublication);

        public static PortfolioManagementEventSource Log
        {
            get
            {
                return _lazyLog.Value;
            }
        }

        [NonEvent]
        public static PortfolioManagementEventSource InitLog()
        {
            var log = new PortfolioManagementEventSource();
            log.Ping();

            return log;
        }

        [Event(1, Level = EventLevel.LogAlways)]
        public void Ping()
        {
            if (IsEnabled())
                WriteEvent(1);
        }

        [Event(2, Level = EventLevel.Informational)]
        public void PotfolioInfo(string args)
        {
            if (IsEnabled())
            {
                WriteEvent(2, args);
            }
        }

        [Event(3, Level = EventLevel.Informational)]
        public void GetPortfoliosCountSuccess(string args)
        {
            if (IsEnabled())
                WriteEvent(3, args);
        }

        [NonEvent]
        public void GetPortfoliosCountError(string args, Exception exception)
        {
            Guard.ArgumentNotNull(exception, "exception");

            if (IsEnabled())
                this.GetPortfoliosCountError(args, exception.ToString());
        }

        [Event(4, Level = EventLevel.Error)]
        public void GetPortfoliosCountError(string args, string exception)
        {
            if (IsEnabled())
                WriteEvent(4, args, exception);
        }

        [Event(5, Level = EventLevel.Informational)]
        public void GetPortfolios(string args)
        {
            if (IsEnabled())
            {
                WriteEvent(5, args);
            }
        }

        [Event(6, Level = EventLevel.Informational)]
        public void GetPortfoliosSuccess(string args)
        {
            if (IsEnabled())
                WriteEvent(6, args);
        }

        [NonEvent]
        public void GetPortfoliosError(string args, Exception exception)
        {
            Guard.ArgumentNotNull(exception, "exception");

            if (IsEnabled())
                this.GetPortfoliosError(args, exception.ToString());
        }

        [Event(7, Level = EventLevel.Error)]
        public void GetPortfoliosError(string args, string exception)
        {
            Guard.ArgumentNotNull(exception, "exception");

            if (IsEnabled())
                WriteEvent(7, args, exception);
        }

        [Event(8, Level = EventLevel.Informational)]
        public void AddPortfolioCompanies(string list)
        {
            if (IsEnabled())
                WriteEvent(8, list);
        }

        [Event(9, Level = EventLevel.Informational)]
        public void AddPortfolioCompaniesSuccess(string list)
        {
            if (IsEnabled())
                WriteEvent(9, list);
        }

        [NonEvent]
        public void AddPortfolioCompaniesError(string args, Exception exception)
        {
            Guard.ArgumentNotNull(exception, "exception");

            if (IsEnabled())
                this.AddPortfolioCompaniesError(args, exception.ToString());
        }

        [Event(10, Level = EventLevel.Error)]
        public void AddPortfolioCompaniesError(string list, string exception)
        {
            if (IsEnabled())
                WriteEvent(10, list, exception);
        }

        [Event(11, Level = EventLevel.Informational)]
        public void AddPortfolio(string portfolio)
        {
            if (IsEnabled())
                WriteEvent(11, portfolio);
        }

        [Event(12, Level = EventLevel.Informational)]
        public void AddPortfolioSuccess(string portfolio)
        {
            if (IsEnabled())
                WriteEvent(12, portfolio);
        }

        [NonEvent]
        public void AddPortfolioError(string args, Exception exception)
        {
            Guard.ArgumentNotNull(exception, "exception");

            if (IsEnabled())
                this.AddPortfolioError(args, exception.ToString());
        }

        [Event(13, Level = EventLevel.Error)]
        public void AddPortfolioError(string portfolio, string exception)
        {
            if (IsEnabled())
                WriteEvent(13, portfolio, exception.ToString());
        }

        [Event(14, Level = EventLevel.Informational)]
        public void GetPortfolioByNameUserId(string args)
        {
            if (IsEnabled())
                WriteEvent(14, args);
        }

        [Event(15, Level = EventLevel.Informational)]
        public void GetPortfolioByNameUserIdSuccess(string args)
        {
            if (IsEnabled())
                WriteEvent(15, args);
        }

        [NonEvent]
        public void GetPortfolioError(string info, Exception exception)
        {
            Guard.ArgumentNotNull(exception, "exception");

            if (IsEnabled())
            {
                PortfolioError(info, exception.GetType().ToString(), exception.Message,
                    ExceptionUtility.GetInfo(exception, true));
            }
        }

        [Event(1001, Level = EventLevel.Error, Message = "Portfolio/Error")]
        public void PortfolioError(string info, string exceptionType, string exceptionMesage, string exceptionInformation)
        {
            if (IsEnabled())
            {
                WriteEvent(1001, info, exceptionType, exceptionMesage, exceptionInformation);
            }
        }

        [Event(17, Level = EventLevel.Informational)]
        public void DeletePortfolio(string id)
        {
            if (IsEnabled())
                WriteEvent(17, id);
        }

        [Event(18, Level = EventLevel.Informational)]
        public void DeletePortfolioSuccess(string id)
        {
            if (IsEnabled())
                WriteEvent(18, id);
        }

        [NonEvent]
        public void DeletePortfolioError(string args, Exception exception)
        {
            Guard.ArgumentNotNull(exception, "exception");

            if (IsEnabled())
                this.DeletePortfolioError(args, exception.ToString());
        }

        [Event(19, Level = EventLevel.Error)]
        public void DeletePortfolioError(string id, string exception)
        {
            if (IsEnabled())
                WriteEvent(19, id, exception.ToString());
        }

        [Event(20, Level = EventLevel.Informational)]
        public void DeletePortfolioCompanyByPortfolioId(string id)
        {
            if (IsEnabled())
                WriteEvent(20, id);
        }

        [Event(21, Level = EventLevel.Informational)]
        public void DeletePortfolioCompanyByPortfolioIdSuccess(string id)
        {
            if (IsEnabled())
                WriteEvent(21, id);
        }

        [NonEvent]
        public void DeletePortfolioCompanyByPortfolioIdError(string args, Exception exception)
        {
            Guard.ArgumentNotNull(exception, "exception");

            if (IsEnabled())
                this.DeletePortfolioCompanyByPortfolioIdError(args, exception.ToString());
        }

        [Event(22, Level = EventLevel.Error)]
        public void DeletePortfolioCompanyByPortfolioIdError(string id, string exception)
        {
            if (IsEnabled())
                WriteEvent(22, id, exception.ToString());
        }

        [Event(23, Level = EventLevel.Informational)]
        public void UpdatePortfolio(string portfolio)
        {
            if (IsEnabled())
                WriteEvent(23, portfolio);
        }

        [Event(24, Level = EventLevel.Informational)]
        public void UpdatePortfolioSuccess(string portfolio)
        {
            if (IsEnabled())
                WriteEvent(24, portfolio);
        }

        [Event(26, Level = EventLevel.Informational)]
        public void UpdatePortfolioStatus(string portfolio)
        {
            if (IsEnabled())
                WriteEvent(26, portfolio);
        }

        [Event(27, Level = EventLevel.Informational)]
        public void UpdatePortfolioStatusSuccess(string portfolio)
        {
            if (IsEnabled())
                WriteEvent(27, portfolio);
        }

        [NonEvent]
        public void UpdatePortfolioStatusError(string args, Exception exception)
        {
            Guard.ArgumentNotNull(exception, "exception");

            if (IsEnabled())
                this.UpdatePortfolioStatusError(args, exception.ToString());
        }

        [Event(28, Level = EventLevel.Error)]
        public void UpdatePortfolioStatusError(string portfolio, string exception)
        {
            if (IsEnabled())
                WriteEvent(28, portfolio, exception.ToString());
        }

        [Event(29, Level = EventLevel.Informational)]
        public void UpdatePortfolioAndPortfolioCompanies(string args)
        {
            if (IsEnabled())
                WriteEvent(29, args);
        }

        [Event(30, Level = EventLevel.Informational)]
        public void UpdatePortfolioAndPortfolioCompaniesSuccess(string args)
        {
            if (IsEnabled())
                WriteEvent(30, args);
        }

        [NonEvent]
        public void UpdatePortfolioAndPortfolioCompaniesError(string args, Exception exception)
        {
            Guard.ArgumentNotNull(exception, "exception");

            if (IsEnabled())
                this.UpdatePortfolioAndPortfolioCompaniesError(args, exception.ToString());
        }

        [Event(31, Level = EventLevel.Error)]
        public void UpdatePortfolioAndPortfolioCompaniesError(string args, string exception)
        {
            if (IsEnabled())
                WriteEvent(31, args, exception.ToString());
        }

        [Event(32, Level = EventLevel.Informational)]
        public void UpdatePortfolioName(string portfolio)
        {
            if (IsEnabled())
                WriteEvent(32, portfolio);
        }

        [Event(33, Level = EventLevel.Informational)]
        public void UpdatePortfolioNameSuccess(string portfolio)
        {
            if (IsEnabled())
                WriteEvent(33, portfolio);
        }

        [NonEvent]
        public void UpdatePortfolioNameError(string args, Exception exception)
        {
            Guard.ArgumentNotNull(exception, "exception");

            if (IsEnabled())
                this.UpdatePortfolioNameError(args, exception.ToString());
        }

        [Event(34, Level = EventLevel.Error)]
        public void UpdatePortfolioNameError(string portfolio, string exception)
        {
            if (IsEnabled())
                WriteEvent(34, portfolio, exception.ToString());
        }
    }
}
